package com.rq.viewtestapplication;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.text.Layout;
import android.text.StaticLayout;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import com.rq.viewtestapplication.manager.OnDoubleClickListener;

public class TextChild extends View implements View.OnClickListener, OnDoubleClickListener {
    public TextChild(Context context) {
        this(context, null, 0);
    }

    public TextChild(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TextChild(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        debug = new TextPaint();
        debug.setColor(Color.RED);
        debug.setTextSize(24);
        setOnClickListener(this);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        postInvalidate();
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        postInvalidate();
    }

    @Override
    public void layout(int l, int t, int r, int b) {
        super.layout(l, t, r, b);
        postInvalidate();
    }

    TextPaint debug;

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        String all = getTag() + "(" + getX() + "," + getY() + ")\nw=" + getWidth() + ",h=" + getHeight();
//        canvas.drawText(all, 0, debugPaint.measureText("66"), debugPaint);
        if (getWidth() < 0) return;
        StaticLayout sl = new StaticLayout(all, debug, getWidth(), Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, true);
        //从0,0开始绘制
        canvas.translate(0, 0);
        sl.draw(canvas);
    }

    @Override
    public void onClick(View v) {
        LOG.e("TextChild", "onClick:" + this.getTag());
    }

    @Override
    public void performDoubleClick() {
        LOG.e("TextChild", "onDoubleClick:" + this.getTag());
    }
}
